using UnityEngine;

public class Skill : MonoBehaviour
{
    public float cooldownTime;
    public float cooldownTimer;
    protected Player Player { get; private set; }
    protected SkillManager SkillManager { get; private set; }

    protected virtual void Awake()
    {
        Player = FindAnyObjectByType<Player>();
        SkillManager = FindAnyObjectByType<SkillManager>();
    }

    protected virtual void Start()
    {
        CheckUnlock();
    }

    protected virtual void Update()
    {
        cooldownTimer -= Time.deltaTime;
    }

    protected virtual void CheckUnlock() { }

    public virtual bool CanUseSkill()
    {
        if (cooldownTimer < 0f)
        {
            Use();
            cooldownTimer = cooldownTime;
            return true;
        }
        Player.Fx.CreatePopUp("Cooldown");
        return false;
    }

    public virtual void Use()
    {
        // Implement skill logic here
    }

    protected Transform FindClosestEnemy(Transform checkTf)
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(checkTf.position, 10);
        float closestDistance = Mathf.Infinity;
        Transform closestEnemyTf = null;
        foreach (Collider2D collider in colliders)
        {
            if (collider.TryGetComponent(out Enemy closestEnemy))
            {
                float distance = Vector2.Distance(checkTf.position, collider.transform.position);
                if (distance < closestDistance)
                {
                    closestDistance = distance;
                    closestEnemyTf = collider.transform;
                }
            }
        }

        return closestEnemyTf;
    }
}